﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;
using Client.Chat;
using System.Timers;

namespace Client
{
    public partial class Form1 : Form
    {
        private Guid clientId;

        private ChatServiceClient chatServiceClient;

        private InstanceContext instanceContext;

        /// <summary>
        /// Constructor.
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ChatServiceCallback chatServiceCallback = new ChatServiceCallback();
            chatServiceCallback.ClientNotified += ChatServiceCallback_ClientNotified;

            instanceContext = new InstanceContext(chatServiceCallback);
            chatServiceClient = new ChatServiceClient(instanceContext);

            try
            {
                clientId = chatServiceClient.Subscribe();
            }
            catch
            {
                // TODO: Handle exception.
            }

            // Set up the timer

            //System.Timers.Timer timer = new System.Timers.Timer(300000);
            //timer.Elapsed +=
            //(
            //    (object o, ElapsedEventArgs args) =>
            //    {
            //        try
            //        {
            //            if (chatServiceClient.State == CommunicationState.Faulted)
            //            {
            //                chatServiceClient.Abort();
            //                chatServiceClient = new ChatServiceClient(instanceContext);
            //            }

            //            chatServiceClient.KeepConnection();
            //        }
            //        catch
            //        {
            //            // TODO: Handle exception.
            //        }
            //    }
            //);
        }

        /// <summary>
        /// Release resources used by the service client and unsubscribes
        /// from the server.
        /// </summary>
        /// <param name="sender">Main window.</param>
        /// <param name="e">Ignored.</param>
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (chatServiceClient != null)
            {
                try
                {
                    if (chatServiceClient.State != CommunicationState.Faulted)
                    {
                        chatServiceClient.Unsubscribe(clientId);
                        chatServiceClient.Close();
                    }
                }
                catch
                {
                    chatServiceClient.Abort();
                }
            }
        }
 

        /// <summary>
        /// Sends the message to the server.
        /// </summary>
        /// <param name="sender">Send button.</param>
        /// <param name="e">Ignored.</param>
        private void button1_Click(object sender, EventArgs e)
        {
            string message = txtMsg.Text;

            if (!string.IsNullOrEmpty(message))
            {
                try
                {
                    if (chatServiceClient.State == CommunicationState.Faulted)
                    {
                        chatServiceClient.Abort();
                        chatServiceClient = new ChatServiceClient(instanceContext);
                    }

                    chatServiceClient.SendMessage(clientId, message);

                    txtMsg.Text = string.Empty;
                }
                catch
                {
                    // TODO: Handle exception
                }
            }
        }


        /// <summary>
        /// Receives a message from the server. Adds the message to the
        /// text box.
        /// </summary>
        /// <param name="sender">ChatServiceCallback object.</param>
        /// <param name="e">Contains the message from the server.</param>
        private void ChatServiceCallback_ClientNotified(object sender, ClientNotifiedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtConference.Text))
            {
                txtConference.Text += "\n";
            }

            txtConference.Text += e.Message;
        }
    }
}
